package com.qf.shixi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qf.shixi.entity.Order;
import com.qf.shixi.result.ResponseCode;
import com.qf.shixi.result.ResponseData;
import com.qf.shixi.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Order)表控制层
 *
 * @author
 * @since 2021-10-11 16:32:20
 */
@Api("预约记录的接口")
@RestController
@RequestMapping("order")
public class OrderController {

//    Logger logger = LoggerFactory.getLogger(OrderController.class);


    @Resource
    private OrderService orderService;
    Logger logger = LoggerFactory.getLogger(OrderController.class);

    @ApiOperation(value = "添加预约记录")
    @PostMapping("/api/insert")
    public ResponseData order(@RequestBody Order order) throws Exception{
        String info = orderService.order(order);
        if(info.equals("success")){
            return new ResponseData();
        }else{
            return new ResponseData(ResponseCode.ERROR);
        }

    }

    @ApiOperation(value = "查询")
    @GetMapping("/queryById/{id}")
    public ResponseData order(@PathVariable Integer id) throws Exception{
        Order order = orderService.queryById(id);
        return new ResponseData(order);
    }

    @ApiOperation(value = "查询")
    @GetMapping("/all")
    public ResponseData queryAll() throws Exception{
        List<Order>  orders = orderService.queryAll();
        return new ResponseData("0","success",orders);
    }

    @ApiOperation(value = "通过id修改预约记录的状态")
    @PatchMapping("/status")
    public ResponseData updateStatus(@RequestBody Order order) {
        Boolean re = orderService.update(order);
        if(re == true){
            return new ResponseData();
        }else{
            return new ResponseData("1009","修改失败");
        }
    }


    @ApiOperation(value = "通过id修改预约记录的信息")
    @PatchMapping("/update")
    public ResponseData update(@RequestBody Order order) {
        Boolean re = orderService.update(order);
        if(re == true){
            return new ResponseData();
        }else{
            return new ResponseData("1009","修改失败");
        }
    }


    @ApiOperation(value = "通过id批量删除数据")
    @DeleteMapping("/dels")
    public ResponseData updateStatus(@RequestBody String ids) {
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray idArr = (JSONArray) jsonObject.get("ids");
        Boolean res = orderService.deleteByIds(idArr);
        if(res == true){
            return new ResponseData();
        }else{
            return new ResponseData("1010","删除失败,请刷新后重试");
        }
    }


    @ApiOperation(value = "条件查询")
    @GetMapping("/con")
    public ResponseData queryByCons(Order order) {
        List<Order> orders = orderService.queryByConditions(order);
        return new ResponseData("0","success",orders);
    }

}



